package org.txazo.blog.web.action.json;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.txazo.blog.commons.cache.Cache;
import org.txazo.blog.service.MemberInfoService;
import org.txazo.blog.service.MemberLoginInfoService;
import org.txazo.blog.util.CookieUtil;
import org.txazo.blog.util.StringUtil;
import org.txazo.blog.util.log.LoggerManager;
import org.txazo.blog.vo.MemberInfo;
import org.txazo.blog.web.action.base.JsonBaseAction;

/**
 * LoginJsonAction
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-12
 * 
 */
@Controller
@Scope("session")
public class LoginJsonAction extends JsonBaseAction {

	private static final long serialVersionUID = 1L;

	/** 默认Refer */
	private static final String DEFAULT_REFER = "http://0.0.0.0";

	/** 邮箱帐号 */
	private String emailAccount;
	/** 密码 */
	private String password;
	/** 验证码 */
	private String authCode;
	/** 是否自动登录 */
	public String autoLogin;

	/** 标识 */
	private int flag = 0;

	@Autowired
	private MemberInfoService memberInfoService;

	@Autowired
	private MemberLoginInfoService memberLoginInfoService;

	@Autowired
	private Cache authCodeCache;

	@Action(value = "isEmailAccountExists", results = { @Result(name = SUCCESS, type = "json") })
	public String isEmailAccountExists() {
		flag = 1;

		return SUCCESS;
	}

	@Action(value = "checkAuthCode", results = { @Result(name = SUCCESS, type = "json") })
	public String checkAuthCode() {
		Object object = authCodeCache.get(getRemoteIP());
		if (object != null) {
			if (authCode.equals((String) object)) {
				flag = 1;
			}
		}

		return SUCCESS;
	}

	@Action(value = "login", results = { @Result(name = SUCCESS, type = "json") })
	public String login() {
		boolean result = memberLoginInfoService.loginValidate(emailAccount,
				password);
		if (result) {
			flag = 1;

			if ("autoLogin".equals(autoLogin)) {
				Map<String, String> valueMap = new HashMap<String, String>();
				valueMap.put("emailAccount", emailAccount);
				valueMap.put("password", password);
				CookieUtil.setCookie(request, response, "autoLogin", valueMap);
			}

			MemberInfo memberInfo = memberInfoService
					.getMemberInfo(emailAccount);
			session.setAttribute("memberInfo", memberInfo);

			String refer = request.getHeader("Referer");
			if (StringUtil.isEmpty(refer)) {
				refer = DEFAULT_REFER;
			}

			LoggerManager.getLoginLogger().info(
					memberInfo.getMemberId() + " " + getRemoteIP() + " "
							+ refer);
		}

		return SUCCESS;
	}

	public void setEmailAccount(String emailAccount) {
		this.emailAccount = emailAccount;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}

	public void setAutoLogin(String autoLogin) {
		this.autoLogin = autoLogin;
	}

	public int getFlag() {
		return flag;
	}

}
